<?php

namespace App\Http\Controllers;

use App\Validation\MemberCollectSearchValidation;
use App\Validation\MemberCollectValidation;
use App\Service\MemberCollectService;
use App\Validation\DeleteValidation;

class MemberCollectController extends BaseController
{
	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberCollectSearchValidation, $this->request->getJsonRawBody());
		$data = MemberCollectService::getList($raw);
		$d = [];
		foreach ($data as $key => $value) {
			$value->json_content = json_decode($value->json_content);
			$d[$key] = $value;
			$d[$key]->json_content->go_url = $value->json_content->go_url;
			$d[$key]->create_time = $this->getShowTime($value->create_time);
		}
		$count = MemberCollectService::getList($raw, TRUE);
		$assign = [
			'total' => $count,
			'list' => $d,
		];

		return $this->echoJson($assign);
	}

	public function detailAction()
	{
		$id = $this->dispatcher->getParam('id');
		$data = MemberCollectService::getInfo($id);
		$data->json_content = json_decode($data->json_content);
		$content = $data->json_content;

		return $this->echoJson($content);
	}

	/**
	 * [doAction 点击收藏 如果收藏即取消收藏，如果没有即收藏]
	 * @return [type] [description]
	 */
	public function doAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberCollectValidation, $this->request->getJsonRawBody());
		$data = MemberCollectService::do($raw);
		
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = MemberCollectService::forever($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [jugeAction 判断是否收藏]
	 * @return [type] [description]
	 */
	public function jugeAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberCollectValidation, $this->request->getJsonRawBody());
		$data = MemberCollectService::getCollectInfo($raw->member_id, $raw->t, $raw->tid);
		if ($data){//收藏
			return $this->echoJson('1');
		}else{//未收藏
			return $this->echoJson('2');
		}
	}
}